package com.example.service;

import com.example.entity.Buzzer;
import com.example.entity.Devices;
import com.example.entity.Params;
import com.example.repository.BuzzerMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class BuzzerService implements DeviceScheduleThreshold, DeviceValueUpdate {

    public static List<Integer> STATUS_BUZZER_LIST;

    static {
        STATUS_BUZZER_LIST = Lists.newArrayList(0);
    }

    @Resource
    private BuzzerMapper buzzerMapper;
    @Resource
    private FlameSensorService flameSensorService;


    public PageInfo<Buzzer> findBuzzer(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Buzzer> search = buzzerMapper.findBuzzer(params);
        return PageInfo.of(search);
    }

    public void addBuzzer(Buzzer buzzer) {
        buzzerMapper.insert(buzzer);
    }

    public void deleteBuzzer(String Bid) {
        buzzerMapper.deleteById(Bid);
    }

    @Override
    public boolean threshold(Devices devices) {
        return Objects.nonNull(devices.getThreshold())
                && flameSensorService.getFire() > devices.getThreshold();
    }

    @Override
    public void statusUpdate(String status) {
        if (StringUtils.equals(status, "1")) {
            STATUS_BUZZER_LIST.set(0,3);
        } else {
            STATUS_BUZZER_LIST.set(0,0);
        }
    }
}
